<template>
  <div class="logistics-container">
    <!-- 顶部导航栏 -->
    <div class="navbar">
      <router-link :to="{ name: 'orders' }" class="navbar-back">
        <i class="fas fa-arrow-left"></i>
      </router-link>
      <div class="navbar-title">物流详情</div>
      <div></div>
    </div>

    <div class="order-info">
      <div class="order-status">
        <i class="fas fa-truck-moving"></i>
        <span>{{ logisticsInfo.status }}</span>
      </div>
      <div class="delivery-info">
        <div class="info-item">
          <label>快递公司：</label>
          <span>{{ logisticsInfo.company }}</span>
        </div>
        <div class="info-item">
          <label>快递单号：</label>
          <span>{{ logisticsInfo.trackingNumber }}</span>
          <button class="copy-btn" @click="copyTrackingNumber">复制</button>
        </div>
      </div>
    </div>

    <!-- 物流轨迹 -->
    <div class="timeline-container">
      <div class="timeline-header">物流轨迹</div>
      <div class="timeline">
        <div 
          v-for="(item, index) in logisticsInfo.tracks" 
          :key="index" 
          class="timeline-item"
          :class="{ 'active': index === 0 }"
        >
          <div class="timeline-point"></div>
          <div class="timeline-content">
            <div class="timeline-title">{{ item.status }}</div>
            <div class="timeline-time">{{ item.time }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 包裹信息 -->
    <div class="package-info">
      <div class="package-header">包裹信息</div>
      <div class="product-card">
        <img class="product-image" :src="logisticsInfo.product.image" :alt="logisticsInfo.product.name">
        <div class="product-details">
          <div class="product-name">{{ logisticsInfo.product.name }}</div>
          <div class="product-attr">{{ logisticsInfo.product.attributes }}</div>
          <div class="product-price-qty">
            <span class="price">¥{{ logisticsInfo.product.price }}</span>
            <span class="quantity">x{{ logisticsInfo.product.quantity }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 收货信息 -->
    <div class="delivery-address">
      <div class="address-header">收货信息</div>
      <div class="address-content">
        <div class="address-row">
          <label>收货人：</label>
          <span>{{ logisticsInfo.address.name }}</span>
        </div>
        <div class="address-row">
          <label>联系电话：</label>
          <span>{{ logisticsInfo.address.phone }}</span>
        </div>
        <div class="address-row">
          <label>收货地址：</label>
          <span>{{ logisticsInfo.address.address }}</span>
        </div>
      </div>
    </div>

    <div class="action-bar">
      <button class="action-button" @click="contactSeller">联系卖家</button>
      <button class="action-button primary" @click="confirmReceipt">确认收货</button>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { showToast, showDialog } from 'vant';
import api from '@/network/api';

export default defineComponent({
  name: 'OrderLogisticsView',
  setup() {
    const route = useRoute();
    const router = useRouter();
    const orderId = route.params.id;
    const logisticsInfo = ref({
      status: '运输中',
      company: '顺丰速运',
      trackingNumber: 'SF1234567890',
      tracks: [
        { status: '【广州市】已到达 广州白云区转运中心', time: '2023-07-15 14:30:25' },
        { status: '【广州市】快件已离开 广州分拣中心，发往 广州白云区转运中心', time: '2023-07-15 10:15:32' },
        { status: '【广州市】快件已到达 广州分拣中心', time: '2023-07-14 23:45:12' },
        { status: '【广州市】快件已由 广州天河区集散中心 揽收，正在发往 广州分拣中心', time: '2023-07-14 18:20:45' },
        { status: '【广州市】卖家已发货', time: '2023-07-14 16:05:30' }
      ],
      product: {
        name: '宠物高级狗粮 全营养配方 10kg装',
        attributes: '牛肉味',
        price: 129,
        quantity: 1,
        image: 'https://images.unsplash.com/photo-1589924691995-400dc9ecc119?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
      },
      address: {
        name: '张三',
        phone: '138****1234',
        address: '广东省广州市海珠区新港中路123号'
      }
    });

    // 获取物流信息
    const fetchLogisticsInfo = async () => {
      try {
        // 这里调用后端API获取物流信息
        // const response = await api.getOrderLogistics(orderId);
        // if (response && response.code === 100) {
        //   logisticsInfo.value = response.data;
        // }
        
        // 使用静态数据，实际项目中替换为API调用
        console.log(`获取订单 ${orderId} 的物流信息`);
        
        // 模拟API调用
        setTimeout(() => {
          showToast('物流信息已更新');
        }, 500);
      } catch (error) {
        console.error('获取物流信息失败:', error);
        showToast('获取物流信息失败');
      }
    };

    // 复制快递单号
    const copyTrackingNumber = () => {
      // 创建临时文本区域
      const textArea = document.createElement('textarea');
      textArea.value = logisticsInfo.value.trackingNumber;
      document.body.appendChild(textArea);
      textArea.select();
      document.execCommand('copy');
      document.body.removeChild(textArea);
      
      showToast('已复制运单号');
    };
    
    // 联系卖家
    const contactSeller = () => {
      showToast('功能开发中，敬请期待');
    };
    
    // 确认收货
    const confirmReceipt = () => {
      showDialog({
        title: '确认收货',
        message: '确认已收到商品吗？确认后无法撤销',
        showCancelButton: true,
        confirmButtonText: '确认收货',
        cancelButtonText: '取消'
      }).then(async (action) => {
        if (action === 'confirm') {
          try {
            // 调用确认收货API
            // const response = await api.confirmReceipt(orderId);
            // if (response && response.code === 100) {
            //   showToast('确认收货成功');
            //   router.replace('/orders');
            // }
            
            // 使用静态数据模拟
            setTimeout(() => {
              showToast('确认收货成功');
              router.replace('/orders');
            }, 500);
          } catch (error) {
            console.error('确认收货失败:', error);
            showToast('确认收货失败，请稍后再试');
          }
        }
      });
    };

    onMounted(() => {
      fetchLogisticsInfo();
    });

    return {
      logisticsInfo,
      copyTrackingNumber,
      contactSeller,
      confirmReceipt
    };
  }
});
</script>

<style scoped>
.logistics-container {
  background-color: #f8fafc;
  min-height: 100vh;
  padding-bottom: 80px;
}

.navbar {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  background-color: white;
  position: relative;
  z-index: 10;
}

.navbar-back {
  color: #4a5568;
  font-size: 16px;
  padding: 5px;
}

.navbar-title {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 500;
  margin: 0 10px;
}

.order-info {
  background-color: white;
  padding: 15px;
  margin-bottom: 10px;
}

.order-status {
  display: flex;
  align-items: center;
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 15px;
  color: #3490dc;
}

.order-status i {
  font-size: 20px;
  margin-right: 10px;
}

.delivery-info {
  color: #4a5568;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.info-item label {
  color: #718096;
  margin-right: 5px;
}

.copy-btn {
  background: none;
  border: 1px solid #cbd5e0;
  border-radius: 12px;
  padding: 2px 8px;
  font-size: 12px;
  color: #4a5568;
  margin-left: 10px;
  cursor: pointer;
}

.timeline-container {
  background-color: white;
  margin-bottom: 10px;
  padding: 15px;
}

.timeline-header, .package-header, .address-header {
  font-weight: 500;
  margin-bottom: 15px;
  font-size: 15px;
}

.timeline {
  position: relative;
}

.timeline:before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 8px;
  width: 1px;
  background-color: #cbd5e0;
}

.timeline-item {
  position: relative;
  padding-left: 30px;
  padding-bottom: 20px;
}

.timeline-point {
  position: absolute;
  left: 5px;
  top: 3px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #cbd5e0;
}

.timeline-item.active .timeline-point {
  background-color: #3490dc;
  width: 10px;
  height: 10px;
  left: 4px;
  top: 2px;
}

.timeline-item.active .timeline-title {
  color: #3490dc;
}

.timeline-content {
  line-height: 1.4;
}

.timeline-title {
  font-size: 14px;
  margin-bottom: 4px;
}

.timeline-time {
  font-size: 12px;
  color: #a0aec0;
}

.package-info {
  background-color: white;
  padding: 15px;
  margin-bottom: 10px;
}

.product-card {
  display: flex;
  padding: 10px 0;
}

.product-image {
  width: 60px;
  height: 60px;
  border-radius: 5px;
  object-fit: cover;
  margin-right: 10px;
}

.product-details {
  flex: 1;
}

.product-name {
  font-size: 14px;
  margin-bottom: 5px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  height: 40px;
}

.product-attr {
  font-size: 12px;
  color: #718096;
  margin-bottom: 5px;
}

.product-price-qty {
  font-size: 12px;
  display: flex;
  justify-content: space-between;
}

.price {
  font-weight: 500;
}

.quantity {
  color: #718096;
}

.delivery-address {
  background-color: white;
  padding: 15px;
  margin-bottom: 10px;
}

.address-content {
  color: #4a5568;
}

.address-row {
  margin-bottom: 8px;
  display: flex;
}

.address-row label {
  color: #718096;
  width: 70px;
}

.action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 10px 15px;
  background-color: white;
  display: flex;
  justify-content: space-around;
  border-top: 1px solid #e2e8f0;
}

.action-button {
  flex: 1;
  padding: 10px;
  border: 1px solid #cbd5e0;
  background-color: white;
  border-radius: 20px;
  margin: 0 5px;
  font-size: 14px;
  color: #4a5568;
}

.action-button.primary {
  background-color: #3490dc;
  color: white;
  border-color: #3490dc;
}
</style> 